<?php

declare(strict_types=1);

namespace app\yperp\command;

use DateTime;
use app\yperp\common\SuosiHttp;
use think\console\Command;
use think\console\Input;

use think\console\Output;

use GuzzleHttp\Client;

use GuzzleHttp\Psr7\Request;

use think\Db;

class baidu extends Command
{
    use SuosiHttp;
    protected $output;

    protected $input;

    protected function configure()
    {
        // 指令配置
        $this->setName('baidu')
            ->setDescription('百度SDK');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $this->input = $input;

        $this->output = $output;

        try {
        //   $res =  $this->go();
        //   dump($res);

          $res =  $this->check();
          dump($res);
        } catch (\Throwable $th) {
            dump($th->getMessage());
            dump($th->getTraceAsString());
        }
    }

    const API_KEY = "bd955833f3964317b1ae49e488b3d8c7";
    const SECRET_KEY = "d683d13add7f4f5082470b507f4aa245";

    private $access_token = '';

    public function go()
    {
        $curl = curl_init();
        curl_setopt_array(
            $curl,
            array(
                CURLOPT_URL => "https://aip.baidubce.com/rpc/2.0/aasr/v1/create?access_token={$this->getAccessToken()}",
                CURLOPT_TIMEOUT => 30,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_SSL_VERIFYHOST => false,
                CURLOPT_CUSTOMREQUEST => 'POST',

                CURLOPT_POSTFIELDS => '{"speech_url":"https://oss-erp.jzzw-tech.cn/u0000486rmf.mhjz10003_01.mp3","format":"mp3","pid":80001,"rate":16000}',

                CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json',
                    'Accept: application/json'
                ),

            )
        );
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }

    public function check() {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://aip.baidubce.com/rpc/2.0/aasr/v1/query?access_token={$this->getAccessToken()}",
            CURLOPT_TIMEOUT => 30,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_SSL_VERIFYHOST  => false,
            CURLOPT_CUSTOMREQUEST => 'POST',
            
            CURLOPT_POSTFIELDS =>'{"task_ids":["6683ba9e5d73020001d4882d"]}',

            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json',
                'Accept: application/json'
            ),

        ));
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }

    /**
     * 使用 AK，SK 生成鉴权签名（Access Token）
     * @return string 鉴权签名信息（Access Token）
     */
    private function getAccessToken()
    {
        $curl = curl_init();
        $postData = array(
            'grant_type' => 'client_credentials',
            'client_id' => "dKb6wnNjSWkP0MZDmRFkhIcV", //self::API_KEY,
            'client_secret' =>"WaevtGDGf5aW5ipkg7QgfDALkVbG3lOx",// self::SECRET_KEY
        );
        curl_setopt_array(
            $curl,
            array(
                CURLOPT_URL => 'https://aip.baidubce.com/oauth/2.0/token',
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_SSL_VERIFYHOST => false,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POSTFIELDS => http_build_query($postData)
            )
        );
        $response = curl_exec($curl);
        curl_close($curl);
        $rtn = json_decode($response);
       
        return $rtn->access_token;
    }
}


